﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PloobsEngine.DataAquisition
{
    public class DataColector 
    {
       
        private Dictionary<Type, List<DataHandlers>> handlersAssociated = new Dictionary<Type, List<DataHandlers>>();
        public void AddHandler(DataHandlers handler)
        {
            foreach (var item in handler.GetHandledType)
            {
                if (handlersAssociated.ContainsKey(item))
                {
                    handlersAssociated[item].Add(handler);
                }
                else
                {
                    handlersAssociated.Add(item, new List<DataHandlers>() { handler });
                }   
            }
            
        }
        public void RemoveHandler(DataHandlers handler)
        {

            foreach (var item in handler.GetHandledType)
            {
                if (handlersAssociated.ContainsKey(item))
                {
                    handlersAssociated[item].Remove(handler);
                }
                else
                {
                    handlersAssociated.Remove(item);
                }  
            }
           
        }

        public void Recieve(DataFormat obj)
        {
                Type type =  obj.GetType();
                if (handlersAssociated.ContainsKey(type ))
                {
                    foreach (var item in handlersAssociated[type])
                    {
                        item.HandleData(obj);
                    }
                    
                }        
        }

       

    }
}
